package com.demo.service.impl;

import com.demo.mapper.UserMapper;
import com.demo.model.SUsers;
import com.demo.service.UserService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;

/**
 * @Author: luobiao
 * @Date: 2024/9/20 14:04
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private PlatformTransactionManager platformTransactionManager;//事务管理器

    @Resource
    private DefaultTransactionDefinition transactionDefinition;

    @Resource
    UserMapper userMapper;


    @Override
    public boolean saveUserInfo(SUsers user) {
        transactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
        try {
            jdbcTemplate.update("INSERT INTO test.s_users ( mobile, name) VALUES (? , ?)", "13025417890","luobiao2");
            userMapper.callMaterialGroupSnapshot();
            //if(1/0==1)
            //jdbcTemplate.execute("call update_user()");
            platformTransactionManager.commit(transaction);
        } catch (Exception ex) {
            platformTransactionManager.rollback(transaction);
            ex.printStackTrace();
        }

        return true;
    }

}
